// scaup - Scala up!
// (c) 2008-2010, Normen Müller, normen.mueller@gmail.com  
// $Id: Logging.scala 78 2010-05-13 12:25:41Z normen.mueller $
package scaup.util

import java.text.MessageFormat.{format}
import org.slf4j.{Logger, LoggerFactory}

/** 
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.4
 */
trait Loggable {  
  val logger:Logger = Logging.getLogger(this)

  def checkFormat(msg:String, refs:Seq[Any]):String = if (refs.size > 0) format(msg, refs) else msg 

  def trace(msg:String, refs:Any*) = logger trace checkFormat(msg, refs)

  def trace(t:Throwable, msg:String, refs:Any*) = logger trace (checkFormat(msg, refs), t)

  def info(msg:String, refs:Any*) = logger info checkFormat(msg, refs)

  def info(t:Throwable, msg:String, refs:Any*) = logger info (checkFormat(msg, refs), t)

  def warn(msg:String, refs:Any*) = logger warn checkFormat(msg, refs)

  def warn(t:Throwable, msg:String, refs:Any*) = logger warn (checkFormat(msg, refs), t)

  def critical(msg:String, refs:Any*) = logger error checkFormat(msg, refs)

  def critical(t:Throwable, msg:String, refs:Any*) = logger error (checkFormat(msg, refs), t)
}

/** 
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.4
 * 
 *  @thanks to http://scalax.scalaforge.org/
 */
object Logging {  
  def loggerNameForClass(className: String) =
    if (className endsWith "$")
      className.substring(0, className.length - 1)
    else className
  
  def getLogger(logging: AnyRef) = LoggerFactory.getLogger(loggerNameForClass(logging.getClass.getName))

}
